﻿namespace USElections.Controls
{
    using System.Diagnostics;
    using System.Windows.Controls;
    using Microsoft.Advertising.Mobile.UI;
    using NorthernLights;
    using USElections.Classes;

    /// <summary>
    /// MyAdControl class.
    /// </summary>
    public partial class MyAdControl : UserControl
    {
        #region Fields
        private string appId;
        private string adUnitId;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the MyAdControl class.
        /// </summary>
        public MyAdControl()
        {
            InitializeComponent();

            if (!AdsManager.IsAdsEnabled)
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.appId = StateVariables.Get<string>("ApplicationId");

                if (string.IsNullOrEmpty(this.appId))
                {
                    Debug.WriteLine("ApplicationId not set.");
                }
            }
        }
        #endregion

        #region Pulic properties
        /// <summary>
        /// Gets or sets the Ad Unit Id.
        /// </summary>
        public string AdUnitId
        {
            get
            {
                return this.adUnitId;
            }

            set
            {
                this.adUnitId = value;
                this.AddAdControl();
            }
        }
        #endregion

        #region Private methods
        private void AddAdControl()
        {
            AdControl adcontrol = new AdControl(this.appId, this.AdUnitId, true);
            adcontrol.Height = 80;
            adcontrol.Width = 480;

            if (this.LayoutRoot.Children.Count != 0)
            {
                this.LayoutRoot.Children.Clear();
            }

            this.LayoutRoot.Children.Add(adcontrol);
        }
        #endregion
    }
}